package com.wyix.stander;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @author wangdx
 * @since 2025/8/1
 */
public class StudentBatchProcessor {
    static class Student {
        String name;
        int age;
        double score;

        Student(String name, int age, double score) {
            this.name = name;
            this.age = age;
            this.score = score;
        }
    }

    public static void main(String[] args) {
        List<Student> students = new ArrayList<>();

        // 1. 从标准输入批量读取学生信息（每行一条，格式：姓名,年龄,成绩）
        try (BufferedReader reader = new BufferedReader(
                new InputStreamReader(System.in))) {

            System.out.println("请输入学生信息（格式：姓名,年龄,成绩，空行结束）：");
            String line;
            while ((line = reader.readLine()) != null && !line.isEmpty()) {
                String[] parts = line.split(",");
                if (parts.length != 3) {
                    System.err.println("跳过无效行：" + line);  // 错误行单独输出
                    continue;
                }
                students.add(new Student(
                        parts[0].trim(),
                        Integer.parseInt(parts[1].trim()),
                        Double.parseDouble(parts[2].trim())
                ));
            }

            // 2. 统计并输出结果到标准输出
            System.out.println("\n===== 批量处理结果 =====");
            System.out.println("总人数：" + students.size());
            double avgScore = students.stream()
                    .mapToDouble(s -> s.score)
                    .average()
                    .orElse(0);
            System.out.printf("平均成绩：%.2f\n", avgScore);

        } catch (IOException e) {
            System.err.println("处理失败：" + e.getMessage());
        }
    }
}
